/*
 * class name: list,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://hcjs.googlecode.com/svn/trunk/hc.js,
 * update: http://hcjs.googlecode.com/svn/trunk/list.js
 *
 */
function list(){/* {str id, elem , list list} */
    var A = arguments,
        _ = this;
    _ = ext(_, ext({
        id: 'listi'+ now(3),
        elem: me.body,
        list: [ { id:'', url:'', ico:'', txt:''} ],
        ajax: {url:'',data:''},
        fn: function(){}
        }, A[0]));
    _.uitype = 'list';
    _.elem = $(_.elem) || me.body;
    _.active = null;
    _.times = {};
}

list.prototype = {

    getlist: function () {
        var _ = this;
        if ( this.ajax && p(this.ajax, 'url') ) {
            var css = aja$( this.ajax, {finish: function(a) {
                    if ( a ) {
                        var y = a.replace(/^\|{1,}|\|{1,}$/g,'').split('|||');
                        for ( var i=0,L=y.length; i<L; i++ ) {
                            if ( typeof y[i]=='string' ) {
                                y[i] = y[i].jsonCss();//[{...},{...}]
                            }
                        }

                        _.add( y );

                    }
                } } );
        } else {
            this.add(this.list);
        }
    return this;
    },

    timer: function() {
        if ( !this.active ) {
            return false;
        }

        this.active.className='a';
    return false;
    },

    reset: function() {
        this.active = null;
        this.times = {};
        this.root.empty();
        this.getlist();
    return this;
    },

    add: function(lis){/* arr lis */
        if( !lis || !lis.length ) {
            return this;
        }

        var _ = this;

        for ( var i=0, L=lis.length; i<L; i++ ) {
            var lo = lis[i];

            if ( !p(lo,'id') ) {
                continue;
            }

            var li = elm('li').appendTo(this.root),
                a = elm('a').appendTo(li);
            a.id = !$(lo.id) ? lo.id : this.id +'_a_'+ now(3);
            a.href = p(lo, 'url') ? lo.url : '#';
            a.innerHTML = lo.txt;

            a.onclick = typeof _.fn=='function'
                ? (function () {
                    if ( _.active && _.active.id==this.id ) {
                        return false;
                    } else if ( _.active ) {
                        _.active.className = '';
                        delete _.times[_.active.id];
                    }

                    _.fn.apply(this, []);
                    _.active = this;
                    _.times[this.id] = now(2);
                    _.timer();
                    return false;
                    })
                : function () {return false;};

            a.ondblclick = function(){
                if ( !_.active || _.active.id!=this.id || typeof _.fn!='function' ) {
                    return false;
                }
                _.fn.apply(this,[]);
                _.times[this.id] = now(2);
            return false;
            };
        }
    return this;
    },

    init: function(){
        this.$ = elm('div').setClass('tvi').appendTo(this.elem);
        this.$.id = this.id = !$(this.id) ? this.id : '_list_'+ this.id;

        this.root = elm('ul').appendTo(this.$);

        this.getlist();
    return this;
    }

};